package com.ug_project.alert

import android.app.Activity
import androidx.fragment.app.Fragment
import android.view.Window
import com.ug_project.Async.runASync
import com.ug_project.views.click

/**
 * Created by yeganaaa on 3/26/2017.
 */
open class AlertBox(window: Window) : Alerter(window) {
    var onClick: (AlertBox) -> Unit = {
        it.close()
    }

    var delayMs: Long = 2000

    override fun onShow() {
        runASync {
            delay(delayMs)
            runUI {
                if (!isClosed)
                close()
            }
        }
        containerView.click = {
            onClick(this)
        }
    }
}

fun Activity.alert(content: String, title: String = "", closed: (() -> Unit)? = null)
{
    runOnUiThread {
        AlertBox(this.window).apply {
            this.title = title
            this.content = content
            this.closedEvent = closed?: {}
        }.show()
    }
}

fun Fragment.alert(content: String, title: String = "", closed: (() -> Unit)? = null) : Unit = this.activity!!.alert(content, title, closed)